﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.ComponentModel;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using NetROS.Tiers.Entities;
using NetROS.Tiers.Data;

#endregion

namespace NetROS.Tiers.Data.SqlClient
{
	///<summary>
	/// This class is the SqlClient Data Access Logic Component implementation for the <see cref="Izvadak"/> entity.
	///</summary>
	[DataObject]
	[CLSCompliant(true)]
	public partial class SqlIzvadakProvider: SqlIzvadakProviderBase
	{
	    private readonly string _constring;
		/// <summary>
		/// Creates a new <see cref="SqlIzvadakProvider"/> instance.
		/// Uses connection string to connect to datasource.
		/// </summary>
		/// <param name="connectionString">The connection string to the database.</param>
		/// <param name="useStoredProcedure">A boolean value that indicates if we use the stored procedures or embedded queries.</param>
		/// <param name="providerInvariantName">Name of the invariant provider use by the DbProviderFactory.</param>
		/// 
        public SqlIzvadakProvider(string connectionString, bool useStoredProcedure, string providerInvariantName)
            : base(connectionString, useStoredProcedure, providerInvariantName)
		{
		    _constring = connectionString;
		}


	    ///<summary>
        ///</summary>
        ///<param name="transactionManager"></param>
        ///<param name="IDs"></param>
        ///<param name="start"></param>
        ///<param name="pageLength"></param>
        ///<param name="count"></param>
        ///<returns></returns>
        public override PretvoreneStavkeIzvatkaList GetStavkeBytblIzvodIDs(TransactionManager transactionManager, String IDs, int start, int pageLength, out int count)
        {
            SqlDatabase database = new SqlDatabase(_constring);
           // DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "[dbo].[_StavkeIzvoda_GetStavkeBytblIzvodIDs]", true);
            DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "[dbo].[_StavkeIzvoda_GetStavkeSIDKlijentomBytblIzvodIDs]", true);
            
            database.AddInParameter(commandWrapper, "@IDs", DbType.String,IDs);
            IDataReader reader = null;

            //Create Collection
            PretvoreneStavkeIzvatkaList rows = new PretvoreneStavkeIzvatkaList();

            try
            {
                //Provider Data Requesting Command Event
            //    OnDataRequesting(new CommandEventArgs(commandWrapper, "GetStavkeBytblIzvodIDs", rows));

                if (transactionManager != null)
                {
                    reader = Utility.ExecuteReader(transactionManager, commandWrapper);
                }
                else
                {
                    reader = Utility.ExecuteReader(database, commandWrapper);
                }

                FillStavke(reader, rows, start, pageLength);
                count = -1;
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt32(0);
                    }
                }

                //Provider Data Requested Command Event
                //OnDataRequested(new CommandEventArgs(commandWrapper, "GetStavkeBytblIzvodIDs", rows));
            }
            finally
            {
                if (reader != null)
                    reader.Close();

                commandWrapper = null;
            }
            return rows;
        }

    
	}
}